package com.zn.kjj.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.zn.kjj.entity.SysLogin;
import com.zn.kjj.entity.SysLoginXtyh;
import com.zn.kjj.service.SysLoginXtyhable;
import com.zn.kjj.service.SysLoginable;
import com.zn.kjj.util.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import com.zn.kjj.entity.PageInfo;


/**
 * @author Administrator
 */
@Controller()
@RequestMapping("/user")
public class UserController {
    @Autowired
    private SysLoginXtyhable sysLoginXtyhImpl;
    @Autowired
    @Qualifier("sysLoginImpl")
    private SysLoginable sysLoginImpl;

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @RequestMapping("/toUserIndex")
    public String toUserIndex(@ModelAttribute("optype") String optype, HttpServletRequest request) {
        List<SysLoginXtyh> ulist = new ArrayList<>();
//        ulist = sysLoginXtyhImpl.selectAll();
//        request.setAttribute("ulist", ulist);
        Page<SysLoginXtyh> persons = sysLoginXtyhImpl.findByPage(1, 10);
        // 需要把Page包装成PageInfo对象才能序列化。该插件也默认实现了一个PageInfo
        PageInfo<SysLoginXtyh> pageInfo = new PageInfo<>(persons);
        logger.debug(pageInfo.toString());
        logger.debug(JSON.toJSONString(pageInfo));
        request.setAttribute("userinfo", pageInfo);
        request.setAttribute("optype", optype);
        return "/user/user_index";
    }

    @RequestMapping("/toUserView{strGuid}")
    public String toUserView(@PathVariable String strGuid, HttpServletRequest request) {
        System.out.println(strGuid);
        SysLoginXtyh sysLoginXtyh = sysLoginXtyhImpl.selectByPrimaryKey(strGuid);
        request.setAttribute("user", sysLoginXtyh);
        return "/user/user_view";
    }

    @RequestMapping("/toUserUpdate{strGuid}")
    public String toUserUpdate(@PathVariable String strGuid, HttpServletRequest request) {
        System.out.println(strGuid);
        SysLoginXtyh sysLoginXtyh = sysLoginXtyhImpl.selectByPrimaryKey(strGuid);
        request.setAttribute("user", sysLoginXtyh);
        return "/user/user_update";
    }

    @RequestMapping("/doUserUpdate")
    public String doUserUpdate(SysLoginXtyh sysLoginXtyh, RedirectAttributes model) {
        System.out.println(sysLoginXtyh.getFztjgzrycz());
        System.out.println("toUserUpdate");
        try {
            sysLoginXtyhImpl.updateByPrimaryKey(sysLoginXtyh);
            model.addAttribute("optype", "操作成功");
        } catch (Exception e) {
            model.addAttribute("optype", "操作失败");
        }
        return "redirect:/user/toUserIndex";
    }

    /**
     * 重置密码
     *
     * @param strGuid
     * @param model
     * @return
     */
    @RequestMapping("/toUserPW{strGuid}")
    public String toUserPW(@PathVariable String strGuid, RedirectAttributes model) {
        int nType = 0;
        try {
            SysLogin sysLogin = sysLoginImpl.selectByPrimaryKey(strGuid);
            if (sysLogin != null) {
                sysLogin.setLoginPass(Tools.getMD5(sysLogin.getLoginName()));
                nType = sysLoginImpl.updateByPrimaryKeySelective(sysLogin);
            }
            if (nType == 0) {
                model.addAttribute("optype", "操作失败");
            } else {
                model.addAttribute("optype", "操作成功");
            }
        } catch (Exception e) {
            model.addAttribute("optype", "操作失败");
        }
        return "redirect:/user/toUserIndex";
    }
}
